package LeetcodeTest_forty;
//1052. 爱生气的书店老板

public class maxSatisfied
{
    public static void main(String[] args)
    {
        int[] customers={1,0,1,2,1,1,7,5};
        int[] grumpy={0,1,0,1,0,1,0,1};
        int X=3;
        System.out.println(solution.maxSatisfied(customers,grumpy,X));
    }
}
class solution
{
    public static int maxSatisfied(int[] customers, int[] grumpy, int X)
    {

        int n=customers.length;
        int answer=0;
        int sum=0;
        int number=0;
        for(int i=0;i<=n-X;i++)
        {
            if(i==0)
            {
                for (int j = i; j < i + X; j++)
                {
                    if (grumpy[j] == 1)
                    {
                        number += customers[j];
                    }
                }
            }
            else
            {
                if(grumpy[i-1]==1)
                    number-=customers[i-1];
                if(grumpy[i+X-1]==1)
                    number+=customers[i+X-1];
            }
            sum=Math.max(sum,number);
            if(grumpy[i]!=1)
                answer+=customers[i];
        }
        for(int i=n-X+1;i<n;i++)
            if(grumpy[i]!=1)
                answer+=customers[i];
        return answer+sum;
    }
}